package mobi.greenplate.theweb.repository;

import mobi.greenplate.theweb.domain.OfferRide;
import java.util.List;
import java.util.ArrayList;
import javax.annotation.security.RolesAllowed;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import mobi.greenplate.theweb.domain.User;
import mobi.greenplate.theweb.user.UserUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository("offerRideRepository")
public class OfferRideRepository {

    private EntityManager entityManager;

    @PersistenceContext
    void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public void delete(Long id) {
        final OfferRide forDelete = entityManager.getReference(OfferRide.class, id);
        entityManager.remove(forDelete);
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public OfferRide save(OfferRide offerRide,User offeredBy) {
        offerRide.setOfferedBy(offeredBy);
        return entityManager.merge(offerRide);
    }
    
    @RolesAllowed(value = {"ROLE_USER"})
    public OfferRide findById(Long id) {
        final OfferRide offerRide = entityManager.find(OfferRide.class, id);
        if(null==offerRide){
            throw new IllegalArgumentException("offerRide with id="+id+" not found");
        }
        return offerRide;        
    }

    public int countAll() {
        return ((Integer) entityManager.createQuery("SELECT count(o) from OfferRide o").
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<OfferRide> findAll(int firstResult, int maxResult) {
        final List<OfferRide> founded = entityManager.createQuery("SELECT o FROM OfferRide o").
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<OfferRide>(founded);
    }
    

    //ManyToOne by transportWith
    public int countByTransportWith(Long transportWithId) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from OfferRide o WHERE o.transportWithId=:transportWithId").
                setParameter("transportWithId", transportWithId).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<OfferRide> findByTransportWith(int firstResult, int maxResult, Long transportWithId) {
        final List<OfferRide> founded = entityManager.createQuery("SELECT o FROM OfferRide o WHERE o.transportWithId=:transportWithId").
                setParameter("transportWithId", transportWithId).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<OfferRide>(founded);
    }    

    //ManyToOne by startAddress
    public int countByStartAddress(Long startAddressId) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from OfferRide o WHERE o.startAddressId=:startAddressId").
                setParameter("startAddressId", startAddressId).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<OfferRide> findByStartAddress(int firstResult, int maxResult, Long startAddressId) {
        final List<OfferRide> founded = entityManager.createQuery("SELECT o FROM OfferRide o WHERE o.startAddressId=:startAddressId").
                setParameter("startAddressId", startAddressId).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<OfferRide>(founded);
    }    

    //ManyToOne by endAddress
    public int countByEndAddress(Long endAddressId) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from OfferRide o WHERE o.endAddressId=:endAddressId").
                setParameter("endAddressId", endAddressId).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<OfferRide> findByEndAddress(int firstResult, int maxResult, Long endAddressId) {
        final List<OfferRide> founded = entityManager.createQuery("SELECT o FROM OfferRide o WHERE o.endAddressId=:endAddressId").
                setParameter("endAddressId", endAddressId).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<OfferRide>(founded);
    }    

    //ManyToOne by offeredBy
    public int countByOfferedBy(Long offeredById) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from OfferRide o WHERE o.offeredById=:offeredById").
                setParameter("offeredById", offeredById).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<OfferRide> findByOfferedBy(int firstResult, int maxResult, Long offeredById) {
        final List<OfferRide> founded = entityManager.createQuery("SELECT o FROM OfferRide o WHERE o.offeredById=:offeredById").
                setParameter("offeredById", offeredById).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<OfferRide>(founded);
    }    


    
    
}
